package guiClasses;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import objectClasses.Database;
import objectClasses.Draft;
import objectClasses.Entry;

public class EntryPanelSmall extends Observable{

	private JPanel my_panel;
	
	public EntryPanelSmall(final Observer the_o_ver, final Entry the_entry, final int the_index) {
		addObserver(the_o_ver);
		my_panel = new JPanel();
		my_panel.setPreferredSize(new Dimension(450,120));
		my_panel.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
		
		my_panel.setLayout(new GridBagLayout());
		my_panel.add(new JLabel(the_entry.getCategory()), GBC.GBCGen(0,0,0,0));
		JButton removeButton = new JButton("Remove");
		removeButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Database.removeBasedOnIndex(GuiMain.getUser().getUserName(), the_index);
				Database.printAllEntries();
				Database.saveFile();
				my_panel.setVisible(false);
				setChanged();
				notifyObservers(new AttendeePanel(the_o_ver).getPanel());
			}
			
		});
		
		my_panel.add(removeButton, GBC.GBCGen(0,0,0,1));
		my_panel.add(new JLabel(the_entry.getCategory()), GBC.GBCGen(0, 0, 0, 0));
		GridBagConstraints c = GBC.GBCGen(1,1,1,0);
		c.gridheight = 2;
		my_panel.add(new JLabel(the_entry.getEntryMaterialsString()), c);
		BufferedImage draftImage = DraftPanel.createImage(the_o_ver, the_entry.getMyDraft());
		c = GBC.GBCGen(0, 0, 2, 0);
		c.gridheight = 2;
		JLabel imageLabel = new JLabel(new ImageIcon(draftImage.getScaledInstance(100, 100, 0)));
		imageLabel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
		imageLabel.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
//				
				Database.removeBasedOnIndex(GuiMain.getUser().getUserName(), the_index);
				Database.printAllEntries();
				Database.saveFile();
				setChanged();
				notifyObservers(new DraftPanel(the_o_ver, the_entry.getMyDraft()).getPanel());
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		my_panel.add(imageLabel, c);
	}
	
	public JPanel getPanel() {
		return my_panel;
	}

}
